package cn.hongwei.controller.admin;


import cn.hongwei.entity.Ad;
import cn.hongwei.result.R;
import cn.hongwei.service.AdService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 广告推荐 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-07
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/cms/ad")
public class AdminAdController {
    @Autowired
    AdService adService;
    //1、查询所有广告
    @GetMapping("list")
    public R listAll(){
        List<Ad> ads = adService.list();
        return R.ok().data("items" , ads);
    }
    //2、带条件分页查询广告
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Page<Ad> page = new Page<>(pageNum , pageSize);
        QueryWrapper<Ad> queryWrapper = new QueryWrapper<>();
        page = adService.page(page,queryWrapper);
        return R.ok().data("page" , page);
    }
    //3、新增广告
    @PostMapping("save")
    public R saveAd(@RequestBody Ad ad){
        adService.save(ad);
        return R.ok();
    }
    //4、删除广告
    @DeleteMapping("remove/{id}")
    public R deleteById(@PathVariable String id){
        adService.removeById(id);

        return R.ok();
    }
    //5、查询指定id的广告
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){
        Ad ad = adService.getById(id);
        return R.ok().data("item" , ad);
    }
    //6、更新广告
    @PutMapping("update")
    public R updateAd(@RequestBody Ad ad){
        adService.updateById(ad);
        return R.ok();
    }


}

